package com.etcn.common.utils.secret;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * AES 是一种可逆加密算法，对用户的敏感信息加密处理 对原始数据进行AES加密后，在进行Base64编码转化；
 */
public class AESUtil {
	
	//偏移量
	private static final String ivParameter = "6122039203853219";
	
	private static AESUtil instance = null;

	private AESUtil() {
	}

	public static AESUtil getInstance() {
		if (instance == null)
			instance = new AESUtil();
		return instance;
	}

	/**
	 * 加密
	 * @param sSrc 原文
	 * @param skey 加密密钥
	 * @return 加密 并进行base64编码的字符串
	 * @throws Exception
	 */
	public String encrypt(String sSrc,String skey) throws Exception {
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		byte[] raw = skey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
		return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。
	}

	/**
	 * 解密
	 * @param sSrc base64编码的加密后的数据
	 * @param skey 解密密钥
	 * @return 原文
	 * @throws Exception
	 */
	public String decrypt(String sSrc,String skey) throws Exception {
		try {
			byte[] raw = skey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, "utf-8");
			return originalString;
		} catch (Exception ex) {
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		
		//加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式，key需要为16位。
		String key = "p8tLm4IHmTTLEWL6";// key，可自行修改
		
		// 需要加密的字串
		String cSrc = "{}";
		System.out.println("加密钱字符串长度:"+cSrc.length());
		// 加密
		long lStart = System.currentTimeMillis();
		String enString = AESUtil.getInstance().encrypt(cSrc,key);
		System.out.println("加密后的字串长度：" + enString.length());

		long lUseTime = System.currentTimeMillis() - lStart;
		System.out.println("加密耗时：" + lUseTime + "毫秒");
		// 解密
		lStart = System.currentTimeMillis();
		String DeString = AESUtil.getInstance().decrypt("laSVVw4t1mY2PSOEmj1oAsBWysvA9h/w5VPHebY9lxZknEvJF1xZ23HxYtKJlef5Qor9wdXmidf6\r\ngPzFwTYNa5DNmvXVwFtUHZlDGbzB8aJvWz9FOa2byofR1T3etzAUX6ePiueENlKYM8o2Grot2qYI\r\nrb6R7oZEMHewqQjUdG1eLdaDkAIaty2oUSm+lvaxFJ1f3i6glypfPkaQqpPuIanbtwwvNcEs+YtQ\r\nN4sKBgkWO6IUYTR0idL4ben1tGE5pniKKOjnidTgkYH4AlMPxM6SJAsz3uiztN5qaNHqOLszfeMn\r\nNWAtWbzWPy6/3/fi9rNVqr3XugW7KnqpLsdz+FBgrfVRLKmyeLLRg3hkSGeBqNg2pOSnba+LtIYt\r\nHBTi8QscPSPvABJK9wrvcYlnY9BXg6s5BorqgFQdcGARLCLBGlaJWxoa+VhgviT1aUzCfdVGLDQt\r\nhX+bzKGMHyRLcwoKWgNXuMX0yuF6n2V+1d2K14frR5B470n7zMwgCJqQOjNRbxqeivf7LrhPgail\r\nsPqX99tFAhT8v370W/nfOig8XcPplAt4OpQasv6pIC+LGIrdIDmhU7SfqjoFXBnOJqUORM4xVuAg\r\nZ9VEKmch07oPC7CSy7/OvDLMPES/KDR5VdjFOWKHQrOnWSn4kdSAxNZ50o3jXkOym3tI1eJjhy7p\r\n/6/Zyo7744pPHeoHNnupC4NkPXgm+aAjPnqjbG7M5o11ycyvQOIXIPX30sezW6SR5tO2fuiGN/UN\r\nBqtXUhVfPBB5qivpwybh7tpbST2mNIYMDKXK3T2xIkhjbBng5G9bLCnR2+LUfujSyQVCYt1ajuim\r\nXD9YRXOSSfFMoYqLZLPvjiyi2TvsDW+AmD3VPhHnk05R6HfJl696FtA0mfXnL1VMx+RhkKrEK1OE\r\n1el+kmWmlBx6ARw7GMNadfQOmPTJoeuT0K7XkCgloQsVulbxxpLqX8+n7uiZpl8pIEuIqjGv9FnV\r\ngfX+o3NxssBdkWTd1vsPNix1OPKqDHQ7DFUU6S8iRxm7fR1c+jwSjMTlHzvhmIHSAyiPp/KCxlrE\r\nTWuK+YiGCHUMx6fF91Ou4SZBMElG1XFOQUghJKQ9ZVzwBRWgg1094HcOLXaXVgn/TfD+GOWP06PW\r\n2BmGINiNjuLqjfUIS+lXIEQ5cIct0SBr+8mHgio+GR3rxNM4OhpE54N5ZoyEzV8KpVpQX+y+BJ4A\r\nv8TTI6Y8mjE0SOY308elNe8cEjNbXnLTFDAR1xJqgTYT5DbAfh6OPI3yr8v0AYVpnvz7u4KtZqlo\r\nvff3X9TR9Xyvys+8SgUOKQO5zZtPZ63lcHUivJ4tjXnaIadp/jQ34qAE+GoqqD3zvx/UHTJx4pdL\r\no1+Ota9vrAc7SKrdy71vx1LQU4VkrBqkKbdu0df89ithVLpG9w8QmxVW5Lwm9ZA6S0cKtJyH8gD2\r\nUg9VrSXidrWxRrCSQhzxJ4HBYIs6TWwQ9IP4S83Svxx7+4RR0fV/6aB+jjr+T8loiuy/fZLoH1Y0\r\nDX3xOBvSHM1ns6eVmRSN6AkvN+TV1Vopsbts7g==","VrFMFQLeEOd6lXhR");
		System.out.println("解密后的字串是：" + DeString);
		lUseTime = System.currentTimeMillis() - lStart;
		System.out.println("解密耗时：" + lUseTime + "毫秒");
	}

}